<?php
/**
 * 过虑
 *
 * @package Filter
 * @author shukyyang
 * @version $Id$
 */
namespace SF;

class Filter
{
    /**
     * 聚合验证处理
     * 形式：
     * runFilter('xxx', 'stringToLower')
     * runFilter('xxx', array(
     *     'stringToLower',
     *     array('filter'=>'deQuote', 'options'=>array(1))
     * ))
     * @param mixed value
     */
    public static function runFilter($value, $rule)
    {
        is_array($rule) || $rule = array($rule);
        foreach ($rule as $f) {
            is_string($f) && $f = array('filter' => $f);
            if (empty($f['filter']) || !method_exists(__CLASS__, $f['filter'])) {
                throw new \SF\Exception('无效的过虑方法' . $f['filter']);
            }
            $options = array('value' => $value);
            if (isset($f['options']) && is_array($f['options'])) {
                $options = array_merge($options, $f['options']);
            }
            $value = call_user_func_array(array(__CLASS__, $f['filter']), $options);
        }
        return $value;
    }

    /**
     * 小写
     */
    public static function stringToLower($value)
    {
        return strtolower((string) $value);
    }

    /**
     * 大写
     */
    public static function stringToUpper($value)
    {
        return strtoupper((string)$value);
    }

    /**
     * trim
     */
    public static function stringTrim($value, $char = ' ')
    {
        return trim($value, $char);
    }

    /**
     * 过虑引号
     */
    public static function deQuote($value, $q = 0)
    {
        if ($q == 0 || $q == 1) {
            $value = str_replace('\'', '', $value);
        }
        if ($q == 0 || $q == 2) {
            $value = str_replace('\"', '', $value);
        }
        return $value;
    }

    /**
     * basename
     */
    public static function basename($value)
    {
        return basename((string)$value);
    }

    /**
     * int
     */
    public static function int($value)
    {
        return (int)$value;
    }

    /**
     * 转义特殊符号
     */
    public static function htmlEntity($value)
    {
        return htmlentities($value, ENT_QUOTES);
    }

    /**
     * 特殊字符安全过滤
     */
    public static function secure($value)
    {
        return str_replace(array('"', '\'', '%', '`', '\\', '/'), '', $value);
    }

    /**
     * 加斜框转义
     */
    public static function addslashes($value)
    {
        if (is_array($value)) {
            foreach ($value as $key=>$val) {
                $key = str_replace(array('"', '\''), '', $key);
                $value[$key] = self::addslashes($val);
            }
        } else {
            $value = addslashes($value);
        }
        return $value;
    }
    
    /**
     * 跳过转义 
     */
    public static function stripslashes($value)
    {
        if (is_array($value)) {
            foreach ($value as $key => $val) {
                $key = str_replace(array('"', '\''), '', $key);
                $value[$key] = self::stripslashes($val);
            }
        } else {
            $value = stripslashes($value);
        }
        return $value;
    }

}